package com.domain;

import java.util.Arrays;

/**
 * 描述 
 * 时间 2017-10-23 20:23
 * 作者 肖亦鸣
 */
public class CircularQueue<E> {

	protected int length;
	protected int front;
	protected int rear;
	protected Object[] queueList;

	public CircularQueue() {
		this.init(3600);
	}

	public CircularQueue(int length) {
		this.init(length);
	}

	private void init(int length) {
		this.length = length;
		front = 0;
		rear = 0;
		queueList =  new Object[this.length];
	}

	public boolean isEmpty() {
		if (front == rear) {
			return true;
		}
		return false;
	}

	public boolean isFull() {
		if (((rear + 1) % length) == front) {
			return true;
		} else {
			return false;
		}
	}
	
	public void enQueue(E e) {
		if (isFull() == false) {
			rear = (rear) % length;
			this.queueList[rear] = e;
			rear ++;
		} else {
			System.out.println("ERROR:Queue is Full");
			return;
		}
	}
	
	@SuppressWarnings("unchecked")
	public E queueFront() {
		E getFront = null;
		if (isEmpty() == false) {
			getFront = (E) queueList[(front + 1) % length];
		} else {
			System.out.println("ERROR:Queue is Empty");
		}
		return getFront;
	}


	
	public void outQueue() {
		if (isEmpty() == false) {
			front = (front + 1) % length;
		} else {
			System.out.println("ERROR:Queue is Empty");
			return;
		}
	}

	@Override
	public String toString() {
		return "CircularQueue [length=" + length + ", front=" + front
				+ ", rear=" + rear + ", queueList="
				+ Arrays.toString(queueList) + "]";
	}
	
	
	
}
